package com.example.myapplication.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.myapplication.App;

public class PreferenceHelper {

    private static PreferenceHelper mInstance;

    private SharedPreferences cache;

    private PreferenceHelper() {
        cache = App.AppContext.getSharedPreferences("foo", Context.MODE_PRIVATE);
    }

    public static PreferenceHelper getInstance() {
        if(mInstance == null)
            mInstance = new PreferenceHelper();
        return mInstance;
    }

    private final String KEY_WORD_DB = "KEY_WORD_DB";
    private final String KEY_START_IDX = "KEY_START_IDX";
    private final String KEY_END_IDX = "KEY_END_IDX";
    private final String KEY_GAP_MS = "KEY_GAP_MS";
    private final String KEY_REPEAT = "KEY_REPEAT";

    public void saveGap(Integer i) {
        cache.edit().putInt(KEY_GAP_MS, i).commit();//同步 sync
    }
    public Integer loadGap() {
        return cache.getInt(KEY_GAP_MS, 1000);
    }
    public void saveRepeat(Integer i) {
        cache.edit().putInt(KEY_REPEAT, i).commit();//同步 sync
    }
    public Integer loadRepeat() {
        return cache.getInt(KEY_REPEAT, 2);
    }
    public void saveWordDB(Integer idx) {
        String db = "CET6";
        if(idx==1) db = "TOEFL"; else if(idx==2) db = "Postgraduate";
        cache.edit().putString(KEY_WORD_DB, db).apply();//异步 async
    }

    public int loadWordDB() {
        String val = cache.getString(KEY_WORD_DB, "CET6");
        return val.equals("CET6") ? 0 :  (val.equals("TOEFL") ? 1 : 2);
    }
    public void saveStartIdx(Integer i) {
        cache.edit().putInt(KEY_START_IDX, i).commit();//同步 sync
    }
    public Integer loadStartIdx() {
        return cache.getInt(KEY_START_IDX, 1);
    }
    public void saveEndIdx(Integer i) {
        cache.edit().putInt(KEY_END_IDX, i).commit();
    }
    public Integer loadEndIdx() {
        return cache.getInt(KEY_END_IDX, 100);
    }
}
